
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>澄一藏</title>
    <link rel="stylesheet" href="{$cases}cases.css">
    <style>
        .table-bordered td:first-child{
            width: 40%;
        }
        td[contenteditable="true"]:focus {
            outline: 2px solid #007bff;
        }
        td>.bootstrap-select {
            width: 100% !important;
            height: 100% !important;
        }
        .table-bordered td:first-child {
            width: 15%;
        }
        .floating-div {
            position: fixed;
            top: 10px;
            right: 10px;
            background-color: #f1f1f1;
            padding: 10px;
        }

        .flaot-button {
            display: block;
            margin-bottom: 5px;
        }
    </style>
</head>

<body>
    <datalist id="appNamelist"></datalist>
    <datalist id="treatmentList">

    </datalist>
    <div class="outer">
<!--        <div class="title">患者详情-诊断</div>-->
        <hr>
        <div class="information">
            <h3>患者信息</h3>
            <div class="floating-div">
                <a class="btn btn-warning   btn-addtab flaot-button" href="?app=cases@index&archives_id={$member_info.id}" data-area="50%,50%" data-title="【{$member_info.name}】的病历记录">往期病历</a>
                {if $wait_info['type']==0}
                <button class="btn btn-success btn-table-tool btn-confirm wati_end flaot-button" href="{url wait atuoAdd}&wait_id={$wait_id}" data-msg="确定要去治疗排队吗？"  data-ajax="1">治疗排号</button>
                {/if}
            </div>
            <table class="table table-bordered">
                <thead class="table-primary">
                <tr>
                    <th>信息</th>
                    <th>详情</th>
                    <th>信息</th>
                    <th>详情</th>
                    <th>信息</th>
                    <th>详情</th>
                </tr>
                </thead>
                <tbody>
<!--                <tr>-->
<!--                    <td>关系:</td>-->
<!--                    <td contenteditable="true" data-field="relation">{? $member_info.relation}</td>-->
<!--                </tr>-->
                <tr>
                    <td>姓名:</td>
                    <td  data-field="name">{? $member_info.name}</td>
                    <td>性别:</td>
                    <td  data-field="sex">{? $member_info.sex}</td>
                    <td>年龄:</td>
                    <td contenteditable="true" id="age" data-field="age">{? $member_info.age}</td>
                </tr>
<!--                <tr>-->
<!--                    <td>性别:</td>-->
<!--                    <td  data-field="sex">{? $member_info.sex}</td>-->
<!--                </tr>-->
<!--                <tr>-->
<!--                    <td>年龄:</td>-->
<!--                    <td contenteditable="true" id="age" data-field="age">{? $member_info.age}</td>-->
<!--                </tr>-->
                <tr>
                    <td>阳历生日:</td>
                    <td contenteditable="true" data-field="birth_date">{? $member_info.birth_date}</td>
                    <td>阴历生日:</td>
                    <td contenteditable="true" data-field="lunar_birth">{? $member_info.lunar_birth}</td>
                    <td>手机号:</td>
                    <td contenteditable="true" data-field="phone">{? $member_info.phone}</td>
                </tr>
<!--                <tr>-->
<!--                    <td>阴历生日:</td>-->
<!--                    <td contenteditable="true" data-field="lunar_birth">{? $member_info.lunar_birth}</td>-->
<!--                </tr>-->
<!--                <tr>-->
<!--                    <td>手机号:</td>-->
<!--                    <td contenteditable="true" data-field="phone">{? $member_info.phone}</td>-->
<!--                </tr>-->
                <tr>
                    <td>身高(cm):</td>
                    <td contenteditable="true" data-field="height">{$member_info.height?''}</td>
                    <td>体重(kg):</td>
                    <td contenteditable="true" data-field="weight">{$member_info.weight?''}</td>
                    <td>民族:</td>
                    <td contenteditable="true" data-field="nation" >{? $member_info.nation}</td>
                </tr>
<!--                <tr>-->
<!--                    <td>体重(kg):</td>-->
<!--                    <td contenteditable="true" data-field="weight">{$member_info.weight?''}</td>-->
<!--                </tr>-->
                </tbody>
            </table>
            <div id="moreInfo" class="collapse">
                <table class="table table-bordered">
                    <tbody>
                    <!-- 添加更多信息 -->
<!--                    <tr>-->
<!--                        <td>民族:</td>-->
<!--                        <td contenteditable="true" data-field="nation">{? $member_info.nation}</td>-->
<!--                    </tr>-->
                    <tr>
                        <td style="width: 10%">学历:</td>
                        <td style="width: 15%">
                            <select name="" id="education" class="selectpicker" >
                                {:options,$education,$member_info.education}
                            </select>

                        </td>
                        <td style="width: 10%">工作性质:</td>
                        <td contenteditable="true" data-field="work" style="width: 20%" >{? $member_info.work}</td>
                        <td style="width: 10%">家庭排行:</td>
                        <td contenteditable="true" data-field="family_rank" style="width: 20%">{? $member_info.family_rank}</td>
                    </tr>
                    <tr>
                        <td>家庭住址:</td>
                        <td contenteditable="true"  id="address" data-field="address" colspan="5">{? $member_info.address}</td>
                    </tr>
                    <tr>
                        <td>婚育情况:</td>
                        <td contenteditable="true" data-field="marriage_childbirth" colspan="5">{? $member_info.marriage_childbirth}</td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#moreInfo">更多信息</button>
<!--            <button type="button" class="btn btn-primary"  >往期病历</button>-->
<!--            <a class="btn btn-primary   btn-addtab" href="?app=cases@index&archives_id={$member_info.id}" data-area="50%,50%" data-title="【{$member_info.name}】的病历记录">往期病历</a>-->
<!--            {if $wait_info['type']==0}-->
<!--                <button class="btn btn-primary btn-table-tool btn-confirm wati_end" href="{url wait atuoAdd}&wait_id={$wait_id}" data-msg="确定要去治疗排队吗？"  data-ajax="1">治疗排号</button>-->
<!--            {/if}-->
        </div>
        <hr>
        <div style="padding: 20px;">
<!--            <ul class="uls">-->
<!--                <li class="tab-t" style="border-color: #797979">诊疗记录</li>-->
<!--                <li class="tab-t">治疗服务</li>-->
<!--                <li class="tab-t">开药服务</li>-->
<!--                <li class="tab-t">调理记录</li>-->
<!--                <li class="tab-t">其他信息</li>-->
<!--            </ul>-->
            <nav class="nav nav-pills flex-column flex-sm-row">
                <li style="cursor:pointer; " class="tab-t  flex-sm-fill text-sm-center nav-link active" >诊疗记录</li>
                <li style="cursor:pointer; " class="tab-t  flex-sm-fill text-sm-center nav-link">治疗服务</li>
                <li style="cursor:pointer; " class="tab-t  flex-sm-fill text-sm-center nav-link ">开药服务</li>
                <li style="cursor:pointer; " class="tab-t  flex-sm-fill text-sm-center nav-link">调理记录</li>
                <li style="cursor:pointer; " class="tab-t  flex-sm-fill text-sm-center nav-link">其他信息</li>
            </nav>
            <hr>
            <!-- model-item tab切换五项 -->
            <div class="model-item" style="display: block">
                <div>
                    <form  role="form" class="epii" method="post" data-form="1" action="{url cases add_edit}">
                        <div class="form-group">
                            <label for="exampleFormControlInput1">主述</label>
                            <textarea name="r[main_desc]" class="form-control"  rows="3" >{? $info.main_desc}</textarea>
                        </div>
                        <div class="form-group">
                            <label >望-面诊</label>
                            <textarea class="form-control" name="r[look_over][face]" rows="2" >{? $info.look_over.face}</textarea>
                        </div>
                        <div class="form-group">
                            <label >望-舌诊</label>
                            <textarea name="r[look_over][tongue]" class="form-control"  rows="2" >{? $info.look_over.tongue}</textarea>
                        </div>
                        <div class="form-group">
                            <label >闻-声音</label>
                            <textarea name="r[smell][sound]" class="form-control"  rows="2" >{? $info.smell.sound}</textarea>
                        </div>
                        <div class="form-group">
                            <label >闻-异味</label>
                            <textarea name="r[smell][taste]" class="form-control"  rows="2" >{? $info.smell.taste}</textarea>
                        </div>

                        <div class="form-group">
                            <label >问</label>
                            <textarea name="r[ask]" class="form-control"  rows="2" >{? $info.ask}</textarea>
                        </div>
                        <div class="form-group">
                            <label >切-脉诊：</label>
                        </div>
                        <div class="form-group">
                            <label >心</label>
                            <textarea type="text" class="form-control" name="r[pulse_taking][heart]" rows="2">{? $info.pulse_taking.heart}</textarea>
                        </div>
                        <div class="form-group">
                            <label >肝</label>
                            <textarea type="text" class="form-control" name="r[pulse_taking][liver]" rows="2">{? $info.pulse_taking.liver}</textarea>
                        </div>
                        <div class="form-group">
                            <label >肾</label>
                            <textarea type="text" class="form-control" name="r[pulse_taking][kidney]" rows="2">{? $info.pulse_taking.kidney}</textarea>
                        </div>
                        <div class="form-group">
                            <label >肺</label>
                            <textarea type="text" class="form-control" name="r[pulse_taking][lung]" rows="2">{? $info.pulse_taking.lung}</textarea>
                        </div>
                        <div class="form-group">
                            <label >脾</label>
                            <textarea type="text" class="form-control" name="r[pulse_taking][spleen]" rows="2">{? $info.pulse_taking.spleen}</textarea>
                        </div>
                        <div class="form-group">
                            <label >命门</label>
                            <textarea type="text" class="form-control" name="r[pulse_taking][life_gate]" rows="2">{? $info.pulse_taking.life_gate}</textarea>
                        </div>
                        <div class="form-group">
                            <label for="exampleFormControlInput1">综合分析</label>
                            <textarea name="r[analysis]" class="form-control"  rows="3" >{? $info.analysis}</textarea>
                        </div>
                        <input type="hidden" name="wait_id" value="{$wait_id}">
                        <input type="hidden" name="case_id" value="{$case_id?0}">
                        <div class="form-footer">
                            <button type="submit" class="btn btn-primary  btn-lg wati_end">保存</button>
                        </div>
                    </form>

                    <!-- <ul class="bottom-list">
                        <li>
                            <div>诊断时间：2023-11-11 13:00:00 </div>
                            <div>所在病房：男1-2床诊断医生：萌小柒&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;></div>
                        </li>
                        <li>
                            <div>诊断时间：2023-11-11 13:00:00 </div>
                            <div>所在病房：男1-2床诊断医生：萌小柒&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;></div>
                        </li>
                        <li>
                            <div>诊断时间：2023-11-11 13:00:00 </div>
                            <div>所在病房：男1-2床诊断医生：萌小柒&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;></div>
                        </li>
                    </ul> -->
                </div>
            </div>
            <div class="model-item">
<!--                <div style="display: flex;justify-content: space-between;margin-bottom: 40px;">-->
<!--                    <div>服务记录（3项）</div>-->
<!--                    <div>所在病房：男1-2床诊断医生：萌小柒2023-11-17 18:00:00</div>-->
<!--                </div>-->
                <div style="display: flex;justify-content: space-between;margin-bottom: 20px;">

                    <div style="width: 100%;">
                        <h3 >治疗服务信息</h3>
                        <button onclick="addTreatment()" class="wati_end" style="margin:20px 0 20px 0;">+新增治疗服务</button>
                        <form  autocomplete="off" role="form" id="treatment_form" class="epii" method="post" data-form="1" action="{url treatment_services add_edit}&case_id={$case_id}">
                            <table style="width: 100%;border-collapse: separate;border-spacing: 5px;" id="treatment_table">
                                <tbody>
                                {if !empty($treatment_services)}
                                {foreach $treatment_services as $k=>$v}
                                <tr>
                                    <td style="width: 16.66%; overflow-wrap: break-word;">
                                        <select onchange="selectCate($(this))"  class="form-control">
                                            {:options,$treatment_cate,$v['treatment_cate_id']}
                                        </select>
                                    </td>
                                    <td style="width: 16.66%; overflow-wrap: break-word;">
                                        <select onchange="selectTreatment($(this))" name="r[treatment_id][]"  class="form-control">
                                            <option value="">请选择服务项</option>
                                            {foreach $v['treatment'] as $m=>$n}
                                            <option value="{$n.id}" data-price="{$n.price}" {if $v['treatment_id']==$n['id']}selected{/if}>{$n.name}</option>
                                            {/foreach}
                                        </select>
                                    </td>
                                    <td style="width: 16.66%; overflow-wrap: break-word;">
                                        <input name="r[price][]" value="{$v.money}" class="form-control" placeholder="价格"   readonly>
                                    </td>
                                    <td>
                                        <button class="btn btn-danger" onclick="delTreatment($(this))">x</button>
                                    </td>
                                </tr>
                                {/foreach}
                                {/if}
                                </tbody>
                            </table>


                            <div class="form-group">
                                <div id="treatment_amount" style="margin:20px 0 20px 0;font-size: 21px; ">合计 项；共计 元。</div>
                            </div>
                            <div class="form-footer">
<!--                                <button type="reset" class="btn btn-default">重置</button>-->
                                <button type="submit" class="btn btn-lg btn-primary wati_end">保存</button>
                            </div>
                        </form>
<!--                        克隆模版-->
                        <table style="display: none">
                            <tr id="treatment_temp">
                                <td style="width: 16.66%; overflow-wrap: break-word;">
                                    <select onchange="selectCate($(this))"  class="form-control">
                                        {:options,$treatment_cate}
                                    </select>
                                </td>
                                <td style="width: 16.66%; overflow-wrap: break-word;">
                                    <select onchange="selectTreatment($(this))" disabled class="form-control" name="r[treatment_id][]">
                                        <option value="">请选择服务项</option>
                                    </select>
                                </td>
                                <td style="width: 16.66%; overflow-wrap: break-word;">
                                    <input name="r[price][]" value="{$v.price}" class="form-control" placeholder="价格"   readonly>
                                </td>
                                <td>
                                    <button class="btn btn-danger" onclick="delTreatment($(this))">x</button>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>

<!--                <ul class="bottom-list">-->
<!--                    <li>-->
<!--                        <div>诊断时间：2023-11-11 13:00:00 </div>-->
<!--                        <div>所在病房：男1-2床诊断医生：萌小柒&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;></div>-->
<!--                    </li>-->
<!--                    <li>-->
<!--                        <div>诊断时间：2023-11-11 13:00:00 </div>-->
<!--                        <div>所在病房：男1-2床诊断医生：萌小柒&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;></div>-->
<!--                    </li>-->
<!--                    <li>-->
<!--                        <div>诊断时间：2023-11-11 13:00:00 </div>-->
<!--                        <div>所在病房：男1-2床诊断医生：萌小柒&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;></div>-->
<!--                    </li>-->
<!--                </ul>-->
            </div>
            <div class="model-item">
                <div style="width: 100%;display: flex;">
                    <div style="width: 60%;">
                        <h3 >药品信息</h3>
                        <button onclick="addRow()" class="wati_end">+添加药品</button>
                        <form  autocomplete="off" role="form" id="prescript_form" class="epii" method="post" data-form="1" action="">
                            <table style="width: 100%;;table-layout:fixed;border-collapse: separate;border-spacing: 2px;" id="table">
                                <tbody>
                                {if !empty($prescript['medicine'])}
                                {foreach $prescript['medicine'] as $k=>$v}
                                <tr>
                                    <td style="width: 16.66%; overflow-wrap: break-word;">
                                        <input list="appNamelist" oninput="medicineSelect($(this))" name="r[name][]" value="{? $v.name}" placeholder="名称/简称" style="height: 50px; width: 100%; padding-left: 10px;">
                                    </td>
                                    <td style="width: 16.66%; overflow-wrap: break-word;">
                                        <input type="number" name="r[quantity][]" oninput="quantity($(this))" value="{? $v.quantity}" placeholder="数量" style="height: 50px; width: 100%; padding-left: 10px;">
                                    </td>
                                    <td style="width: 16.66%; overflow-wrap: break-word;">
                                        <input name="r[unit][]" value="{$v.unit}" placeholder="单位" style="height: 50px; width: 100%; padding-left: 10px;" readonly>
                                    </td>
                                    <td style="width: 16.66%; overflow-wrap: break-word;">
                                        <input name="r[price][]" value="{$v.price}" placeholder="单价" style="height: 50px; width: 100%; padding-left: 10px;" readonly>
                                    </td>
                                    <td style="width: 16.66%; overflow-wrap: break-word;">
                                        <input name="r[subtotal][]" value="{$v.subtotal}"  placeholder="小计" style="height: 50px; width: 100%; padding-left: 10px;" readonly>
                                    </td>
                                    <td style="display: none; width: 16.66%; overflow-wrap: break-word;">
                                        <input type="hidden" name="r[id][]" value="{$v.medicine_id}" placeholder="id" style="height: 50px; width: 100%; padding-left: 10px;">
                                    </td>
                                    <td>
                                        <button class="btn btn-danger" onclick="delTableTr($(this))">x</button>
                                    </td>
                                </tr>
                                {/foreach}
                                {/if}
                                </tbody>

                            </table>
<!--                            <table style="width: 100%;;table-layout:fixed;" id="table">-->
<!--                            </table>-->
                            <div style="margin-top: 20px">
                                <label for="">开药副数:</label>
                                <div class="form-group">
                                    <input type="number" name="r[prescript_num]" class="form-control" id="prescript_num" value="{? $prescript.prescript_num}" />
                                </div>
                            </div>
                            <div style="margin-top: 20px">
                                <label for="">医嘱信息:</label>
                                <div class="form-group">
                                    <textarea name="r[doctor_advice]" class="form-control" id="texta" rows="3" >{? $prescript.doctor_advice}</textarea>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div style="width: 40%;">
                        <div style="font-size: 20px;padding-bottom: 20px;color: #333333;font-weight: 500;">
                            处方信息预览
                        </div>
                        <div class="border">
                            <div class="pad-left">处方编号{$prescript.num}</div>
                            <h3 class="h3">澄一藏中医诊所处方笺</h3>
                            <div style="display: flex;width: 100%;" class="pad-left">
                                <div class="wid20"><span>姓名:</span>&nbsp;<span class="size14">{? $member_info.name}</span></div>
                                <div class="wid20"><span>性别:</span>&nbsp;<span class="size14">{? $member_info.sex}</span></div>
                                <div class="wid20"><span>年龄:</span>&nbsp;<span class="size14">{? $member_info.age}</span></div>
                                <div class="wid40"><span>日期:</span>&nbsp;<span class="size14">{? $prescript.addtime}</span></div>
                            </div>
                            <hr>
                            <div class="pad-left title-list">
                                <div class=""><span>住址:</span>&nbsp;<span class="size14">{? $member_info.address}</span></div>
<!--                                <div class="wid40"><span>日期:</span>&nbsp;<span class="size14">{? $prescript.addtime}</span></div>-->
                            </div>
                            <hr>
<!--                            <div class="pad-left"><span>诊断：</span><span class="size14"></span></div>-->
<!--                            <hr>-->
                            <div id="content" style="min-height: 300px;">
                                <table class="table table-bordered" id="prescript_note">
                                    <thead class="table-primary">
                                        <th>名称</th>
                                        <th>数量</th>
                                        <th>单位</th>
                                        <th>单价</th>
                                        <th>小计</th>
                                    </thead>
                                    <tbody>

                                    </tbody>
                                </table>
                            </div>
                            <div class="pad-left"><span>医嘱信息：</span><span id="advice" class="size14">{? $prescript.doctor_advice}</span></div>
                            <hr>
                            <div class="pad-left">
                                <div><span>医师签名：</span><span class="size14">{? $prescript.admin_name}</span></div>
                            </div>
                            <hr>
                            <div class="pad-left"><span>总金额：</span><span id="total-amount" class="size14"></span></div>
                        </div>
                    </div>
                </div>
                <div style="display: flex;justify-content: flex-end;align-items: center;padding: 30px 10px;">
                    <span id="total" style="margin-right: 10px;">共计：0种药品，合计0元</span>
                    <button  id="submit_btn" class="wati_end">提交</button>
                </div>
<!--                <div class="form-footer">-->
<!--                    <button type="submit" class="btn btn-primary  btn-lg btn-block">保存</button>-->
<!--                </div>-->
<!--                <ul class="bottom-list">-->
<!--                    <li>-->
<!--                        <div>诊断时间：2023-11-11 13:00:00 </div>-->
<!--                        <div>所在病房：男1-2床诊断医生：萌小柒&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;></div>-->
<!--                    </li>-->
<!--                    <li>-->
<!--                        <div>诊断时间：2023-11-11 13:00:00 </div>-->
<!--                        <div>所在病房：男1-2床诊断医生：萌小柒&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;></div>-->
<!--                    </li>-->
<!--                    <li>-->
<!--                        <div>诊断时间：2023-11-11 13:00:00 </div>-->
<!--                        <div>所在病房：男1-2床诊断医生：萌小柒&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;></div>-->
<!--                    </li>-->
<!--                </ul>-->
            </div>
            <div class="model-item">
<!--                <div style="width: 100%;display: flex;margin-bottom: 30px;">-->
<!--                    <div style="width: 40%;">调理时间</div>-->
<!--                    <div style="width: 60%;">症状变化分析</div>-->
<!--                </div>-->
<!--                <hr>-->
                <form  role="form" class="epii" method="post" data-form="1" action="{url recuperate add_edit}&case_id={$case_id}">
<!--                    <div class="form-group">-->
<!--                        <label for="">调理时间</label>-->
<!--                        <input type="datetime-local" name="" class="form-control" >-->
<!--                    </div>-->
                    <div class="form-group">
                        <label for="">症状变化分析</label>
                        <textarea name="r[content]" class="form-control" id="" cols="10" rows="5">{$recuperate.content}</textarea>
                    </div>
                    <div class="form-footer">
<!--                        <button type="reset" class="btn btn-default">重置</button>-->
                        <button type="submit" class="btn btn-lg btn-primary wati_end">保存</button>
                    </div>
                </form>
                <div style="width: 100%;display: flex;margin-bottom: 20px;">
                </div>
<!--                <div style="width: 100%;display: flex;margin-bottom: 20px;">-->
<!--                    <div style="width: 30%;">2023-11-122023-11-14</div>-->
<!--                    <div style="width: 70%;">症状变化分析症状变化分析症状变化分析症状变化分析症状变化分析症状变化分析症状变化分析症状变化分析症状变化分析</div>-->
<!--                </div>-->
<!--                <div style="width: 100%;display: flex;margin-bottom: 20px;">-->
<!--                    <div style="width: 30%;">2023-11-122023-11-14</div>-->
<!--                    <div style="width: 70%;">症状变化分析症状变化分析症状变化分析症状变化分析症状变化分析症状变化分析症状变化分析症状变化分析症状变化分析</div>-->
<!--                </div>-->
            </div>
            <div class="model-item">
                <div>
                    <img src="images/1700817010033.jpg" alt="">
                </div>
            </div>
        </div>
    </div>
</body>

</html>

<script>
    window.onEpiiInit(function() {
        let wait_status={$wait_info.status}
        //已经结束/取消的问诊 禁止修改,禁用提交保存按钮
        if(wait_status==3||wait_status==4){
            $('.wati_end').prop("disabled", true)
        }
        // 开药服务 表单提交
        $("#submit_btn").click(function(event) {
            event.preventDefault(); // 阻止按钮的默认行为

            let formData = $("#prescript_form").serializeArray(); // 获取表单数据
            let age=parseInt($('#age').text());
            let address=$('#address').text();
            let total_price=parseFloat($('#total-amount').text());
            if(!isNaN(age)){
                formData.push({name: "r[age]", value: age});
            }
            formData.push({name: "r[address]", value: address});
            formData.push({name: "r[total_price]", value: isNaN(total_price)?0:total_price});
            let load=layer.load();
            $.ajax({
                url: "{url prescript add_edit}&wait_id={$wait_id}&case_id={$case_id}", // 请求 URL
                type: "POST", // 请求方式
                data: formData, // 请求数据
                dataType: 'json',
                success: function(res) {
                    layer.close(load)
                    layer.alert(res.msg,{icon:res.code});
                    if(res.code){
                        location.reload()
                    }
                    return;
                }
            });
        });

        //监听 患者信息 输入编辑
        $('td[contenteditable="true"]').on('blur', function() {
            let field_value = $(this).text();
            if ($.trim(field_value).length==0){
                layer.msg('输入为空',function () {
                    // window.location.reload()
                })
                return
            }
            let field= $(this).data('field');
            let id={? $info.archives_id};
            let url="{url archives archivesEdit}";
            if(field=='age'){
                id={$case_id};
                url="{url cases ageEdit}"
            }
            // 将编辑后的值保存到数据源中
            let load=layer.load();
            $.ajax({
                url: url, // 请求 URL
                type: "POST", // 请求方式
                data: {'field':field,'field_value':field_value,'id':id}, // 请求数据
                dataType: 'json',
                success: function(res) {
                    console.log(res);
                    layer.close(load)
                    layer.alert(res.msg,{icon:res.code});
                    return;
                }
            });
        });

        //监听 患者信息 下拉选择编辑
        $('#education').change(function() {
            let field_value = $(this).val();
            if ($.trim(field_value).length==0){
                return
            }
            let id={$info.archives_id};
            let load=layer.load();
            // ajax
            $.ajax({
                url: "{url archives archivesEdit}", // 请求 URL
                type: "POST", // 请求方式
                data: {'field':'education','field_value':field_value,'id':id}, // 请求数据
                dataType: 'json',
                success: function(res) {
                    layer.close(load)
                    layer.alert(res.msg,{icon:res.code});
                    return;
                }
            });
        });
        //药品处方统计
        clog();
        //治疗项目 统计
        amountTreatment()
    });
    //药品列表数据
    let drugs = {$medicine_list}
    // 获取元素
    let lis = document.querySelectorAll('.tab-t');
    let models = document.querySelectorAll('.model-item');

    let relatedDrugs = []
    let appNamelist = document.getElementById('appNamelist')
    for (let index = 0; index < drugs.length; index++) {
        let option = document.createElement("option");
        option.innerText = drugs[index].name
        // option.value = drugs[index].id
        appNamelist.appendChild(option)
    }
    // 遍历所有选项卡
    for (let i = 0, len = lis.length; i < len; i++) {
        // 设置当前元素的index索引值
        lis[i].setAttribute('data-index', i);
        // 绑定点击事件
        lis[i].onclick = function () {
            // layer.alert('请先对前置操作进行保存')
            // 将所有选项卡边框设置透明
            for (let j = 0, len = lis.length; j < len; j++) {
                // lis[j].style.borderColor = 'transparent';
                lis[j].classList.remove('active')
            }
            // 将所有选项卡内容设置隐藏
            for (let j = 0, len = models.length; j < len; j++) {
                models[j].style.display = 'none';
            }
            // 设置当前被点击选项卡边框色
            // this.style.borderColor = '#797979';
            this.classList.add("active");
            // 获取被点击选项卡索引
            let index = this.getAttribute('data-index');
            // 显示被点击选项卡所匹配内容
            models[index].style.display = 'block';
        }
    }
    let yaoList = ['名称/简称', '数量', '单位', '单价', '小计','id']
    let nameList = ['r[name][]', 'r[quantity][]', 'r[unit][]', 'r[price][]', 'r[subtotal][]','r[id][]']
    // 添加行数药品
    let table = document.getElementById("table");
    // let addTbody = document.getElementById("addList");
    let rows = [];
    function addRow() {
        let row = document.createElement("tr");
        let rowData = {};

        for (let i = 0; i < nameList.length; i++) {
            let cell = document.createElement("td");
            let input = document.createElement("input");

            if (i === 0) {
                input.setAttribute('list', 'appNamelist');
                // 对第一个输入框添加事件监听器
                input.addEventListener('input', function (index) {
                    return function () {
                        let value = this.value;
                        if(alreadySelect(this.value)){
                            $(this).parent().parent().find('input').val('');
                            layer.alert('药品不能重复选择',{icon:2});
                            return false;
                        }
                        relatedDrugs = drugs.filter(function (drug) {
                            return drug.name.indexOf(value) !== -1;
                        });
                        let rowData = rows[index];
                        let input1 = rowData['input1'];
                        let input2 = rowData['input2'];
                        let input3 = rowData['input3'];
                        let input5 = rowData['input5'];
                        input1.value = relatedDrugs[0].quantity;
                        input2.value = relatedDrugs[0].unit;
                        input2.readOnly=true;
                        input3.value = relatedDrugs[0].price;
                        input3.readOnly=true;
                        rowData['input4'].readOnly=true;
                        input5.value = relatedDrugs[0].id;
                    };
                }(rows.length)); // 传递当前行的索引
            }
            if (i===5){
                input.setAttribute('type','hidden');
                cell.style.display='none';
            }
            input.setAttribute('name', nameList[i]);
            input.addEventListener('input', updateDisplay);
            input.addEventListener('input', clog);
            input.placeholder = yaoList[i];
            input.style.height = '50px';
            input.style.width = '100%';
            input.style.paddingLeft = '10px';
            cell.style.width = "16.66%";
            cell.style.wordWrap = 'break-word';
            cell.appendChild(input);
            row.appendChild(cell);
            rowData['input' + i] = input;
        }

        let deleteButton = document.createElement("button");
        deleteButton.innerText = "x";
        deleteButton.type = "button";
        deleteButton.classList.add('btn-danger');
        deleteButton.onclick = function () {
            let index = row.rowIndex;//table index
            console.log(index);
            table.deleteRow(index);
            rows.splice(index, 1);
            updateInputs();
            clog()
        };
        let deleteCell = document.createElement("td");
        deleteCell.appendChild(deleteButton);
        row.appendChild(deleteCell);
        table.appendChild(row);
        rows.push(rowData);
        updateInputs();
    }
    //删除药品
    function delTableTr(obj){
        obj.parent().parent().remove();
        // updateInputs();
        clog()
    }
    //监听药品选择
    function medicineSelect(obj){
        let value = obj.val();
        console.log(value)
        relatedDrugs = drugs.filter(function (drug) {
            return drug.name.indexOf(value) !== -1;
        });
        let inputs=obj.parent().parent().find('input');
        inputs[1].value = relatedDrugs[0].quantity;
        inputs[2].value = relatedDrugs[0].unit;
        inputs[3].value = relatedDrugs[0].price;
        inputs[4].value = parseInt(relatedDrugs[0].quantity) * parseFloat(relatedDrugs[0].price);
        inputs[5].value = relatedDrugs[0].id;
        clog();
    }
    //监听数量修改
    function quantity(obj){
        let quantity = obj.val();

        let inputs=obj.parent().parent().find('input');
        relatedDrugs = drugs.filter(function (drug) {
            return drug.name.indexOf( inputs[0].value) !== -1;
        });
        console.log(quantity,'quantity')
        console.log(relatedDrugs[0].price,'price')
        result4 = parseInt(quantity) * parseFloat(relatedDrugs[0].price);
        inputs[4].value = isNaN(result4)?'':result4;
        // inputs[5].value = relatedDrugs[0].id;
        clog()
    }
    //判断药品是否已经选择
    function alreadySelect(selctName){
        let tmp=0;
        $('#prescript_form input[name="r[name][]"]').each(function() {
            if (selctName===$(this).val()){
                tmp++;
            }
        });
        return tmp > 1;
    }
    // 更新数量和价格相乘生成小计
    function updateDisplay() {
        for (let i = 0; i < rows.length; i++) {
            let rowData = rows[i];
            let input1 = rowData['input1'];
            let input2 = rowData['input2'];
            let input3 = rowData['input3'];
            let input4 = rowData['input4'];
            let value1 = input1.value;
            let value2 = input3.value;
            let result = parseFloat(value1) * parseFloat(value2);
            input4.value = isNaN(result) ? '' : result;
        }
    }

    function updateInputs() {
        inputs = [];
        for (let i = 0; i < rows.length; i++) {
            let rowData = rows[i];
            for (let j = 0; j < 5; j++) {
                inputs.push(rowData['input' + j]);
            }
        }
    }
    // 开药右边动态创建table处方笺药品
    function clog(){
        //
        var clonedTbody = $(table).find('tr').clone();
        clonedTbody.find('*').removeAttr('style')
        clonedTbody.find('td').addClass('wid20')
        let total_acount=0;//总金额
        let medicine_count=0;//药品种类数
        clonedTbody.each(function() {
            let tr = $(this);
            medicine_count++
            // 遍历该 <tr> 元素下的每个 <td> 元素
            tr.find('td').each(function() {
                let td = $(this);
                let input = td.find('input');
                if (input.length > 0) {
                    let value = input.val();
                    if(input.attr('name')=='r[subtotal][]'){
                        if (!isNaN(parseFloat(value))){
                            total_acount+=parseFloat(value)
                        }
                    }
                    td.text(value);
                    input.remove();
                }
            });
            tr.find('td').slice(-2).remove();
        });
        //医嘱信息

        // 将克隆的 <tr> 添加到目标 <table> 的末尾
        $('#prescript_note').find('tbody').empty();
        $('#prescript_note').append(clonedTbody);
        //同步总金额+药品数量处方笺展示
        $('#total-amount').text(total_acount.toFixed(2));
        $('#total').text(`共计：${medicine_count}种药品，合计${total_acount.toFixed(2)}元`);


    }
    let texta = document.getElementById('texta')
    let advice = document.getElementById('advice')
    texta.addEventListener('input', function () {
        console.log(texta.value);
        advice.innerText = texta.value
    });

</script>
<script>
    //监听治疗服务分类select
    function selectCate(obj){
        // 获取所选分类的值
        let cate_id = obj.val();
        //获取治疗服务select
        let treatment=obj.parent().parent().find('select').eq(1)
        // 清空服务下拉框的选项
        treatment.empty().append('<option value="">请选择服务项</option>');
        // 根据所选分类动态加载治疗服务选项
        if (cate_id) {
            let load=layer.load();
            $.ajax({
                url: "{url treatment getTreatmentList}", // 请求 URL
                type: "POST", // 请求方式
                data: {'treatment_cate_id':cate_id}, // 请求数据
                dataType: 'json',
                success: function(res) {
                    layer.close(load)

                    $.each(res,function (index,value) {
                        treatment.append('<option value="' + value.id + '" data-price="' + value.price + '">' + value.name + '</option>');
                    })
                    treatment.prop('disabled', false);
                    return;
                }
            });

        } else {
            treatment.prop('disabled', true);
        }

    }
    //监听 治疗项 select
    function selectTreatment(obj){
        let price=obj.find('option:selected').data('price')
        obj.parent().parent().find('input').val(price);
        amountTreatment();
    }
    //添加治疗项
    function addTreatment(){
        let trs=$('#treatment_temp').clone();
        trs.removeAttr('style');
        trs.find('input').val('');
        console.log(trs)
        $('#treatment_table').append(trs);
    }
    //删除治疗性
    function delTreatment(obj){
        obj.parent().parent().remove();
        amountTreatment();
    }
    //治疗项 总计
    function amountTreatment(){
        //治疗项数量
        let count=$('#treatment_table tr').length;
        //总金额
        let total=0;
        $("#treatment_table input[name='r[price][]']").each(function (index,value) {
            let price=parseFloat($(value).val())
            if(!isNaN(price)){
                total+=price
            }

        })
        $('#treatment_amount').text(`合计${count}项；共计${total}元。`)
    }

</script>
